#!/bin/sh

. /lib/functions.sh


get_apmode(){
        config_get apver $1 apver "error"
	local old_if_apv1=""
	if [ "$apver" == "error"  ]; then
		### ap ver from single to multi
		if [ -e /etc/config/natshell  ]; then
			uci set apmode.apmode.apver='1'
			uci commit apmode
			old_is_apv1=`uci get natshell.AC.ac_ipaddr 2>/dev/null || echo error`
		fi

		if [ -e /etc/config/nsapd  ]; then
			uci set apmode.apmode.apver='2'
			uci commit apmode
		fi
		if [ "$old_is_apv1" != "0.0.0.0" -a "$old_is_apv1" != "error" -a "$old_is_apv1" != ""  ]; then
			## have configured
			uci set apmode.apmode.apver='1'
			uci commit apmode
		fi
	fi
	config_get acip_user $1 acip_user "error"
	local cur_apver=`uci get apmode.apmode.apver`
	local cur_acip=""
	if [ "$acip_user" == "error" -a "$cur_apver" != "" ]; then
		if [ "$cur_apver" == "2" ]; then
			cur_acip=`uci get nsapd.ac_host.host`
		elif [ "$cur_apver" == "1" ]; then
			cur_acip=`uci get natshell.AC.ac_ipaddr`
		fi
		uci set apmode.apmode.acip_user="${cur_acip:-"0.0.0.0"}"
		uci set apmode.apmode.acip_opt43="0.0.0.0"
		uci set apmode.apmode.acip_dns="0.0.0.0"
		uci set apmode.apmode.dns_enable="1"
		uci set apmode.apmode.opt43_enable="1"
		uci commit apmode
	fi
	config_get switch_intf $1 switch_intf "error"
	if [ -e /etc/config/switch_mode_mul -a "$switch_intf" == "error" ]; then
		## need init the switch_intf
		uci set apmode.apmode.switch_intf="single"
		uci commit apmode
	fi
}

### get ap mode
config_load apmode
config_foreach get_apmode apmode


exit 0
